package sg.edu.nus.iss.billing.ui.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import javax.swing.*;

import sg.edu.nus.iss.billing.service.login.CallAgent;
import sg.edu.nus.iss.billing.service.login.UserImpl;
import sg.edu.nus.iss.billing.ui.controller.*;

public class MainForm extends JFrame {


	private static final long serialVersionUID = 1L;
	
    private JDesktopPane desktop;
    private JMenu mnuAccount;
    private JMenuBar mnuBar;
    private JMenu mnuBilling;
    private JMenuItem mnuExit;
    private JMenuItem mnuLogin;
    private JMenu mnuFile;
    private JMenuItem mnuGenerateBills;
    private JMenuItem mnuRegister;
    private JMenuItem mnuViewAccount;
    
	private OneBillingSystem onebillingsystem;
	
    public MainForm(OneBillingSystem onebillingsystem) {
        this.onebillingsystem = onebillingsystem;
        initComponents();
        setBackground(Color.lightGray);
        setTitle("One Billing System");
        
    }
    GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension screenSize = tk.getScreenSize();
    int screenHeight = screenSize.height;
    int screenWidth = screenSize.width;
     void initComponents() {

       
        desktop = new JDesktopPane();
        mnuBar = new JMenuBar();
        mnuFile = new JMenu();
        mnuLogin = new JMenuItem();
        mnuExit = new JMenuItem();
        mnuAccount = new JMenu();
        mnuRegister = new JMenuItem();
        mnuViewAccount = new JMenuItem();
        mnuBilling = new JMenu();
        mnuGenerateBills = new JMenuItem();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setMaximizedBounds(e.getMaximumWindowBounds());
        setExtendedState( this.getExtendedState()|JFrame.MAXIMIZED_BOTH );

        
        desktop.setMinimumSize(new java.awt.Dimension(2147483647, 2147483647));
        desktop.setBackground(new Color(179,196,222));
        
        
        mnuLogin.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X,  java.awt.event.InputEvent.CTRL_MASK));
        mnuExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.CTRL_MASK));
        mnuRegister.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_R,  java.awt.event.InputEvent.CTRL_MASK));
        mnuViewAccount.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_M,  java.awt.event.InputEvent.CTRL_MASK));
        mnuGenerateBills.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_G,  java.awt.event.InputEvent.CTRL_MASK));
        
        mnuFile.setText("File");

        mnuExit.setText("Exit");
        mnuExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuExitActionPerformed(evt);
            }
        });
        
        
        mnuLogin.setText("Logout");
        mnuFile.add(mnuLogin);
        mnuLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuLoginActionPerformed(evt);
            }
        });
        
        mnuFile.add(mnuExit);
        mnuBar.add(mnuFile);

        mnuAccount.setText("Account");
        mnuAccount.setVisible(false);

        mnuRegister.setText("Register Account");
        mnuRegister.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuRegisterActionPerformed(evt);
            }
        });
        mnuAccount.add(mnuRegister);

        mnuViewAccount.setText("Manage Account");
        mnuAccount.add(mnuViewAccount);

        mnuBar.add(mnuAccount);
        
        mnuViewAccount.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	mnuViewAccountActionPerformed(evt);
            }
        });
      
        mnuBilling.setText("Billing");
        mnuBilling.setVisible(false);

        mnuGenerateBills.setText("Generate Bills");
        mnuGenerateBills.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuGenerateBillsActionPerformed(evt);
            }
        });
        mnuBilling.add(mnuGenerateBills);

        mnuBar.add(mnuBilling);

        setJMenuBar(mnuBar);
        

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(2, 2, 2)
                .addComponent(desktop, GroupLayout.PREFERRED_SIZE, screenWidth, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(desktop, GroupLayout.Alignment.CENTER, GroupLayout.PREFERRED_SIZE, screenHeight, Short.MAX_VALUE)
        );
        
   
        this.getContentPane().setBackground(new Color(0,50,255));
        pack();
    }

    private void mnuExitActionPerformed(java.awt.event.ActionEvent evt) {
    	System.exit(1);
    }


    private void mnuRegisterActionPerformed(java.awt.event.ActionEvent evt) {
        CustomerWindow customer = new CustomerWindow(onebillingsystem,1);
        desktop.add(customer);
        customer.show();
    }
    
    private void mnuViewAccountActionPerformed(java.awt.event.ActionEvent evt) {
        CustomerWindow customer = new CustomerWindow(onebillingsystem,0);
        desktop.add(customer);
        customer.show();
    }


    private void mnuGenerateBillsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuGenerateBillsActionPerformed
        GenerateBill genBill = new GenerateBill();
        desktop.add(genBill);
        genBill.show();
        
    }

    
    private void mnuLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuViewBillsActionPerformed
       setVisible(false);
       LoginWindow loginwindow = new LoginWindow(onebillingsystem,this);
       loginwindow.show();
    }
    
    public JDesktopPane getDesktop(){
    	return desktop;
    }
    
    public void SetMenus(UserImpl user){
    	if(user.getClass() == CallAgent.class){
    		mnuBilling.setVisible(false);
    		mnuAccount.setVisible(true);
    		mnuRegister.setVisible(false);
    	}
    	else{
    		mnuBilling.setVisible(true);
    		mnuAccount.setVisible(true);
    	}
    	setBounds(0, 0,1000, 800);
    }
}
